package me.zhengjie.modules.bushutongji.service.impl;

import me.zhengjie.modules.bushutongji.domain.Bushutongji;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.bushutongji.repository.BushutongjiRepository;
import me.zhengjie.modules.bushutongji.service.BushutongjiService;
import me.zhengjie.modules.bushutongji.service.dto.BushutongjiDTO;
import me.zhengjie.modules.bushutongji.service.dto.BushutongjiQueryCriteria;
import me.zhengjie.modules.bushutongji.service.mapper.BushutongjiMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

/**
* @author xiexidong 
* @date 2019-07-24
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BushutongjiServiceImpl implements BushutongjiService {

    @Autowired
    private BushutongjiRepository bushutongjiRepository;

    @Autowired
    private BushutongjiMapper bushutongjiMapper;

    @Override
    public Object queryAll(BushutongjiQueryCriteria criteria, Pageable pageable){
        Page<Bushutongji> page = bushutongjiRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);

        return PageUtil.toPage(page.map(bushutongjiMapper::toDto));
    }

    @Override
    public Object queryAll(BushutongjiQueryCriteria criteria){
        return bushutongjiMapper.toDto(bushutongjiRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public BushutongjiDTO findById(Integer id) {
        Optional<Bushutongji> bushutongji = bushutongjiRepository.findById(id);
        ValidationUtil.isNull(bushutongji,"Bushutongji","id",id);
        return bushutongjiMapper.toDto(bushutongji.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BushutongjiDTO create(Bushutongji resources) {
        return bushutongjiMapper.toDto(bushutongjiRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Bushutongji resources) {
        Optional<Bushutongji> optionalBushutongji = bushutongjiRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalBushutongji,"Bushutongji","id",resources.getId());
        Bushutongji bushutongji = optionalBushutongji.get();
        bushutongji.copy(resources);
        bushutongjiRepository.save(bushutongji);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        bushutongjiRepository.deleteById(id);
    }

    @Override
    public List<String> findDanwei() {
        return bushutongjiRepository.findDanwei();
    }
}